package com.ikun.lease.web.admin.service.impl;

import com.ikun.lease.common.result.ResultCodeEnum;
import com.ikun.lease.model.enums.ItemType;
import com.ikun.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.ikun.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.ikun.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.ikun.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.ikun.lease.web.admin.vo.fee.FeeValueVo;
import com.ikun.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ikun.lease.common.exception.LeaseException;
import com.ikun.lease.model.entity.*;
import com.ikun.lease.web.admin.mapper.*;
import com.ikun.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentLabelService labelService;


    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private FacilityInfoMapper acilityMapper;
    @Resource
    private ApartmentFeeValueMapper FeeValueMapper;
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //1.查询apartinfo
        ApartmentInfo apartmentInfo = super.getById(id);

        if(apartmentInfo == null){
            return null;
        }
        //2.查询grapinfo
        List<GraphVo> graphVos = graphInfoMapper.getDetailById(ItemType.APARTMENT,id);
        //3.查询Label
        List<LabelInfo> labelInfos = labelInfoMapper.getDetailById(id);
        //4.查询Facility
        List<FacilityInfo> facilityList = acilityMapper.getDetailById(id);
        //5.查询Fee
        List<FeeValueVo> feeValueVoList = FeeValueMapper.getDetailById(id);


        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityList);
        apartmentDetailVo.setGraphVoList(graphVos);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean b = apartmentSubmitVo.getId() != null;//修改数据
        super.saveOrUpdate(apartmentSubmitVo);
        if(b){
            //1.删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoWrapper);
            //2.删除配套列表
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityWrapper);
            //3.删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            labelService.remove(labelLambdaQueryWrapper);
            //4.删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaQueryWrapper<>();
            feeValueWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueWrapper);
        }
        //1.插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfolist = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setId(apartmentSubmitVo.getId());
                graphInfolist.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfolist);
        }
        //2.插入配套列表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            for (Long id : facilityInfoIds) {
                ApartmentFacility Facility = new ApartmentFacility();
                Facility.setApartmentId(apartmentSubmitVo.getId());
                Facility.setFacilityId(id);
                apartmentFacilities.add(Facility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }
        //3.插入标签列表
        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIdList)){
            ArrayList<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long aLong : labelIdList) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(aLong);
                apartmentLabelList.add(apartmentLabel);
            }
            labelService.saveBatch(apartmentLabelList);
        }

        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> apartmentFeeList = new ArrayList<>();
            for (Long fLong : feeValueIds) {
                ApartmentFeeValue apartmentfee = new ApartmentFeeValue();
                apartmentfee.setApartmentId(apartmentSubmitVo.getId());
                apartmentfee.setFeeValueId(fLong);
                apartmentFeeList.add(apartmentfee);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeList);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageApartment(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartment(page, queryVo);
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId,id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if(count>0){
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }
        super.removeById(id);
        //1.删除graphinfo
        LambdaQueryWrapper<GraphInfo> graphWrapper = new LambdaQueryWrapper<>();
        graphWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphWrapper);
        //1.删除labelinfo
        LambdaQueryWrapper<ApartmentLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        labelService.remove(lambdaQueryWrapper);
        //1.删除Feeinfo
        LambdaQueryWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaQueryWrapper<>();
        feeValueWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(feeValueWrapper);
        //1.删除Facilityinfo
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(facilityQueryWrapper);
    }

}




